package ui;

import utils.AndroidUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class ConfigDialog extends JDialog {

    public static final int ITEM_HEIGHT = 32;
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JPanel header;
    private JScrollPane scroll;
    private JPanel list;

    public ConfigDialog(String[] configs) {
        AndroidUtils.centerConfigDialog(this, 800, 500);
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        scroll.setPreferredSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        scroll.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS));
        setTitle("环境配置");
        // 创建表格头部
        createHeader(configs);
        setConfigData();
        //创建表格底部
        createFooter();
    }

    private String mSelectFile;

    @SuppressWarnings("all")
    private void createHeader(String[] configs) {
        header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
        header.add(Box.createHorizontalStrut(2));

        JLabel name = new JLabel("配置环境选择：");
        name.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        // 字体格式 粗细 大小
        name.setFont(new Font("Dialog", Font.BOLD, 16));
        name.setForeground(Color.white);

        header.add(name);
        header.add(Box.createHorizontalGlue());
        JComboBox<String> jc = new JComboBox<>(configs);
        Object selectedItem = jc.getSelectedItem();//返回当前所选项
        if (selectedItem instanceof String) {
            mSelectFile = ((String) selectedItem);
        }
        //添加事件，当列表框中当前选项的数据被发生改变时会触发此事件
        jc.setFont(new Font("Dialog", Font.BOLD, 16));
        jc.setForeground(Color.white);
        jc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mSelectFile = ((String) jc.getSelectedItem());
                System.out.println(" files in buildConfig directory " + mSelectFile);
            }
        });

        jc.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        header.add(jc);
        header.add(Box.createHorizontalStrut(10));
    }

    public static final String HTTP_PORT = "HTTP服务器端口";
    public static final String HTTP_HOST = "HTTP服务器主机名";
    public static final String MSTP_PORT = "MSTP服务器端口";
    public static final String MSTP_HOST = "MSTP服务器主机名";
    public static final String HTTP_CPRY = "HTTP加密公钥串";
    public static final String MSTP_CPRY = "MSTP加密公钥串";
    public static final String HTTP_SE = "HTTP_se文件";
    public static final String MSTP_SE = "MSTP_se文件";
    public static final String CONFIG_FILE = "config_file";


    private JTextField mHttpPort = null;
    private JTextField mHttpHost = null;
    private JTextField mMstpPort = null;
    private JTextField mMstpHost = null;
    private JTextField mHttpCpry = null;
    private JTextField mMstpCpry = null;
    private JTextField mHttpSe = null;
    private JTextField mMstpSe = null;

    private void setConfigData() {
        //HTTP服务器端口
        //HTTP服务器主机名
        //MSTP服务器端口
        //MSTP服务器主机名
        //HTTP加密公钥串
        //MSTP加密公钥串
        //HTTP-se文件
        //MSTP-se文件

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        panel.add(Box.createHorizontalStrut(2));

        //HTTP服务器端口
        JLabel name = new JLabel("HTTP服务器端口:");
        name.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        panel.add(name);
        name.setFont(new Font("Dialog", Font.BOLD, 16));
        name.setForeground(Color.white);
        panel.add(Box.createHorizontalGlue());

        // 输入框
        mHttpPort = new JTextField(16);
        mHttpPort.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        panel.add(mHttpPort);
        mHttpPort.setFont(new Font("Dialog", Font.BOLD, 16));
        mHttpPort.setForeground(Color.white);
        panel.add(Box.createHorizontalStrut(10));

        list.add(panel);

        JPanel httpHostPanel = new JPanel();
        httpHostPanel.setLayout(new BoxLayout(httpHostPanel, BoxLayout.X_AXIS));
        httpHostPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        httpHostPanel.add(Box.createHorizontalStrut(2));

        //HTTP服务器主机名
        JLabel httpHost = new JLabel("HTTP服务器主机名:");
        httpHost.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        httpHostPanel.add(httpHost);
        httpHost.setFont(new Font("Dialog", Font.BOLD, 16));
        httpHost.setForeground(Color.white);
        httpHostPanel.add(Box.createHorizontalGlue());

        // 输入框
        mHttpHost = new JTextField(16);
        mHttpHost.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        httpHostPanel.add(mHttpHost);
        mHttpHost.setFont(new Font("Dialog", Font.BOLD, 16));
        mHttpHost.setForeground(Color.white);
        httpHostPanel.add(Box.createHorizontalStrut(10));
        list.add(httpHostPanel);

        JPanel mstpProtPanel = new JPanel();
        mstpProtPanel.setLayout(new BoxLayout(mstpProtPanel, BoxLayout.X_AXIS));
        mstpProtPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        mstpProtPanel.add(Box.createHorizontalStrut(2));
        //MSTP服务器端口
        JLabel mstpProt = new JLabel("MSTP服务器端口:");
        mstpProt.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpProtPanel.add(mstpProt);
        mstpProt.setFont(new Font("Dialog", Font.BOLD, 16));
        mstpProt.setForeground(Color.white);
        mstpProtPanel.add(Box.createHorizontalGlue());

        // 输入框
        mMstpPort = new JTextField(16);
        mMstpPort.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpProtPanel.add(mMstpPort);
        mMstpPort.setFont(new Font("Dialog", Font.BOLD, 16));
        mMstpPort.setForeground(Color.white);
        mstpProtPanel.add(Box.createHorizontalStrut(10));
        list.add(mstpProtPanel);

        JPanel mstpHostPanel = new JPanel();
        mstpHostPanel.setLayout(new BoxLayout(mstpHostPanel, BoxLayout.X_AXIS));
        mstpHostPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        mstpHostPanel.add(Box.createHorizontalStrut(2));
        //MSTP服务器主机名
        JLabel mstpHost = new JLabel("MSTP服务器主机名:");
        mstpHost.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpHostPanel.add(mstpHost);
        mstpHost.setFont(new Font("Dialog", Font.BOLD, 16));
        mstpHost.setForeground(Color.white);
        mstpHostPanel.add(Box.createHorizontalGlue());

        // 输入框
        mMstpHost = new JTextField(16);
        mMstpHost.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpHostPanel.add(mMstpHost);
        mMstpHost.setFont(new Font("Dialog", Font.BOLD, 16));
        mMstpHost.setForeground(Color.white);
        mstpHostPanel.add(Box.createHorizontalStrut(10));
        list.add(mstpHostPanel);

        JPanel httpCryPanel = new JPanel();
        httpCryPanel.setLayout(new BoxLayout(httpCryPanel, BoxLayout.X_AXIS));
        httpCryPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        httpCryPanel.add(Box.createHorizontalStrut(2));
        //HTTP加密公钥串
        JLabel httpCry = new JLabel("HTTP加密公钥串:");
        httpCry.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        httpCryPanel.add(httpCry);
        httpCry.setFont(new Font("Dialog", Font.BOLD, 16));
        httpCry.setForeground(Color.white);
        httpCryPanel.add(Box.createHorizontalGlue());

        // 输入框
        mHttpCpry = new JTextField(16);
        mHttpCpry.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        httpCryPanel.add(mHttpCpry);
        mHttpCpry.setFont(new Font("Dialog", Font.BOLD, 16));
        mHttpCpry.setForeground(Color.white);
        httpCryPanel.add(Box.createHorizontalStrut(10));
        list.add(httpCryPanel);

        JPanel mstpCryPanel = new JPanel();
        mstpCryPanel.setLayout(new BoxLayout(mstpCryPanel, BoxLayout.X_AXIS));
        mstpCryPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        mstpCryPanel.add(Box.createHorizontalStrut(2));
        //MSTP加密公钥串
        JLabel mstpCry = new JLabel("MSTP加密公钥串:");
        mstpCry.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpCryPanel.add(mstpCry);
        mstpCry.setFont(new Font("Dialog", Font.BOLD, 16));
        mstpCry.setForeground(Color.white);
        mstpCryPanel.add(Box.createHorizontalGlue());

        // 输入框
        mMstpCpry = new JTextField(16);
        mMstpCpry.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpCryPanel.add(mMstpCpry);
        mMstpCpry.setFont(new Font("Dialog", Font.BOLD, 16));
        mMstpCpry.setForeground(Color.white);
        mstpCryPanel.add(Box.createHorizontalStrut(10));
        list.add(mstpCryPanel);

        JPanel httpSePanel = new JPanel();
        httpSePanel.setLayout(new BoxLayout(httpSePanel, BoxLayout.X_AXIS));
        httpSePanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        httpSePanel.add(Box.createHorizontalStrut(2));
        //HTTP-se文件
        JLabel httpSe = new JLabel("HTTP_se文件:");
        httpSe.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        httpSePanel.add(httpSe);
        httpSe.setFont(new Font("Dialog", Font.BOLD, 16));
        httpSe.setForeground(Color.white);
        httpSePanel.add(Box.createHorizontalGlue());

        // 输入框
        mHttpSe = new JTextField(16);
        mHttpSe.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        httpSePanel.add(mHttpSe);
        mHttpSe.setFont(new Font("Dialog", Font.BOLD, 16));
        mHttpSe.setForeground(Color.white);
        httpSePanel.add(Box.createHorizontalStrut(10));
        list.add(httpSePanel);

        JPanel mstpSePanel = new JPanel();
        mstpSePanel.setLayout(new BoxLayout(mstpSePanel, BoxLayout.X_AXIS));
        mstpSePanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        mstpSePanel.add(Box.createHorizontalStrut(2));
        //MSTP-se文件
        JLabel mstpSe = new JLabel("MSTP_se文件:");
        mstpSe.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpSePanel.add(mstpSe);
        mstpSe.setFont(new Font("Dialog", Font.BOLD, 16));
        mstpSe.setForeground(Color.white);
        mstpSePanel.add(Box.createHorizontalGlue());

        // 输入框
        mMstpSe = new JTextField(16);
        mMstpSe.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        mstpSePanel.add(mMstpSe);
        mMstpSe.setFont(new Font("Dialog", Font.BOLD, 16));
        mMstpSe.setForeground(Color.white);
        mstpSePanel.add(Box.createHorizontalStrut(10));
        list.add(mstpSePanel);
    }

    private final HashMap<String, String> configs = new HashMap<>();


    private void createFooter() {
        buttonOK.addActionListener(e -> performGenerate());
        buttonCancel.addActionListener(e -> dispose());

        buttonOK.setFont(new Font("Dialog", Font.BOLD, 16));

        buttonCancel.setFont(new Font("Dialog", Font.BOLD, 16));
    }

    //回调到主类中
    private void performGenerate() {
        dispose();
        setData();
        if (onGenerateListener != null) {
            onGenerateListener.onGenerate(configs);
        }
    }

    private void setData() {
        configs.put(CONFIG_FILE, mSelectFile);
        configs.put(HTTP_PORT, mHttpPort.getText());
        configs.put(HTTP_HOST, mHttpHost.getText());
        configs.put(MSTP_PORT, mMstpPort.getText());
        configs.put(MSTP_HOST, mMstpHost.getText());
        configs.put(HTTP_CPRY, mHttpCpry.getText());
        configs.put(MSTP_CPRY, mMstpCpry.getText());
        configs.put(HTTP_SE, mHttpSe.getText());
        configs.put(MSTP_SE, mMstpSe.getText());
    }

    private OnGenerateListener onGenerateListener;

    public void setOnGenerateListener(OnGenerateListener onGenerateListener) {
        this.onGenerateListener = onGenerateListener;
    }

    public interface OnGenerateListener {
        void onGenerate(HashMap<String, String> hashMap);
    }
}
